package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.entity.UserRole;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.RoleService;
import com.zzyl.utils.EmptyUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @FileName RoleServiceImpl
 * @Description
 * @Author Sword
 * @date 2025-10-08
 **/

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 角色分页
     *
     * @param roleDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<RoleVo> page(RoleDto roleDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<RoleVo> page = roleMapper.page(roleDto);
        PageResponse<RoleVo> roleVoPageResponse = PageResponse.of(page, RoleVo.class);
        return roleVoPageResponse;
    }

    /**
     * 保存角色
     *
     * @param roleDto 角色DTO对象
     * @return RoleVo
     */
    @Override
    public void createRole(RoleDto roleDto) {
        //对象拷贝
        Role role = BeanUtil.copyProperties(roleDto, Role.class);
        roleMapper.insert(role);
    }

    /**
     * 根据角色查询选中的资源数据
     *
     * @param roleId
     * @return
     */
    @Override
    public Set<String> getResourceNoByRoleId(Long roleId) {
        return roleResourceMapper.getResourceNoByRoleId(roleId);
    }

    /**
     * 角色修改
     *
     * @param roleDto
     * @return
     */
    @Override
    @Transactional
    public Boolean updateRole(RoleDto roleDto) {
        //对象拷贝
        Role role = BeanUtil.copyProperties(roleDto, Role.class);

        //todo 角色是否关联用户
        List<UserRole> userRoleList = userRoleMapper.getByRoleId(roleDto.getId());
        if (!EmptyUtil.isNullOrEmpty(userRoleList)) {
            throw new RuntimeException("当前角色下有用户关联，请先解除关联关系");
        }

        roleMapper.updateByPrimaryKeySelective(role);

        //是否包含资源数据
        if (ObjectUtil.isEmpty(roleDto.getCheckedResourceNos()) &&
                ObjectUtil.isNotEmpty(roleDto.getRoleName())) {
            return true;
        }

        //删除当前角色关联的资源
        roleResourceMapper.deleteByRoleId(role.getId());

        //构建角色资源集合
        List<RoleResource> roleResourceList = new ArrayList<>();
        Arrays.asList(roleDto.getCheckedResourceNos()).forEach(resourceNo -> {
            RoleResource roleResource = RoleResource.builder()
                    .roleId(role.getId())
                    .resourceNo(resourceNo)
                    .dataState(SuperConstant.DATA_STATE_0)
                    .build();
            roleResourceList.add(roleResource);
        });

        //判断资源关系是否为空
        if (EmptyUtil.isNullOrEmpty(roleResourceList)) {
            return true;
        }

        //批量插入资源数据
        roleResourceMapper.batchInsert(roleResourceList);

        return true;
    }

    /**
     * 删除角色
     *
     * @param roleId
     */
    @Override
    @Transactional
    public void deleteById(Long roleId) {
        //删除角色资源关联信息
        roleResourceMapper.deleteByRoleId(roleId);

        //删除角色信息
        roleMapper.deleteByPrimaryKey(roleId);
    }

    /**
     * 角色下拉框
     *
     * @return
     */
    @Override
    public List<RoleVo> initRoles() {
        List<RoleVo> roles = roleMapper.list();
        return roles;
    }
}
